﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CRMWeb.Pages.Configuration
{
    public partial class WebServiceConfiguration : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string ServiceID = "";
            bool BlankPage = true;
            try
            {
                ServiceID=Request.QueryString["ServiceID"].ToString();
                BlankPage = false;
            }
            catch (Exception)
            {
                BlankPage = true;
            }
            if (!IsPostBack)
            {
                if (ServiceID != "" && BlankPage == false)
                {
                    DataLoad();
                }
            }
        }
        public void DataLoad()
        {
            CRMEntity.Configuration.WebServiceConfiguration webservice = CRMBiz.Configuration.WebServiceConfiguration.GetSingleWebService(Convert.ToInt32(Request.QueryString["ServiceID"].ToString()));
            PopulateWebserviceForm(webservice);        
        }
        private void PopulateWebserviceForm(CRMEntity.Configuration.WebServiceConfiguration webservice)
        {
            txtServiceName.Text = webservice.ServiceName;
            txtServiceURL.Text = webservice.ServiceURL;
            txtServiceXML.Text = webservice.ServiceInputXML;
           

            if (webservice.ParameterList.Rows.Count > 0)
            {
                grdConfigurationParameter.DataSource = webservice.ParameterList;
                grdConfigurationParameter.DataBind();
            }
            else
            {
                webservice.ParameterList.Rows.Add(webservice.ParameterList.NewRow());
                grdConfigurationParameter.DataSource = webservice.ParameterList;
                grdConfigurationParameter.DataBind();

                int TotalColumns = grdConfigurationParameter.Rows[0].Cells.Count;
                grdConfigurationParameter.Rows[0].Cells.Clear();
                grdConfigurationParameter.Rows[0].Cells.Add(new TableCell());
                grdConfigurationParameter.Rows[0].Cells[0].ColumnSpan = TotalColumns;
                grdConfigurationParameter.Rows[0].Cells[0].Text = "No Record Found";
            }

        }
        
        protected void btnSaveWebService_Click(object sender, EventArgs e)
        {           
            CRMEntity.Configuration.WebServiceConfiguration webserviceconfig = new CRMEntity.Configuration.WebServiceConfiguration();
            webserviceconfig.ServiceName = txtServiceName.Text;
            webserviceconfig.ServiceURL = txtServiceURL.Text;
            webserviceconfig.ServiceInputXML = txtServiceXML.Text;
            string ServiceID = "";
            try
            {
                ServiceID = Request.QueryString["ServiceID"].ToString();
                webserviceconfig.ServiceID = Convert.ToInt32(ServiceID);
            }
            catch (Exception)
            {
                ServiceID = "";
            }
            if (ServiceID=="")
            {
                CRMBiz.Configuration.WebServiceConfiguration.InsertServiceConfiguration(webserviceconfig);       
            }
            else
            {
                CRMBiz.Configuration.WebServiceConfiguration.UpdateWebService(webserviceconfig);       
            }
        }

        protected void grdConfigurationParameter_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Insert"))
            {
                CRMEntity.Configuration.WebServiceParmater NewParameter = new CRMEntity.Configuration.WebServiceParmater();

                TextBox txtNewParameterName = (TextBox)grdConfigurationParameter.FooterRow.FindControl("txtNewParameterName");
                TextBox txtNewParameterReplaceString = (TextBox)grdConfigurationParameter.FooterRow.FindControl("txtNewParameterReplaceString");
                TextBox txtNewParameterValue = (TextBox)grdConfigurationParameter.FooterRow.FindControl("txtNewParameterValue");
                CheckBox chkNewIsDynamic = (CheckBox)grdConfigurationParameter.FooterRow.FindControl("chkNewIsDynamic");

                NewParameter.ParameterName = txtNewParameterName.Text;
                NewParameter.ParameterReplaceString = txtNewParameterReplaceString.Text;
                NewParameter.ParameterValue = txtNewParameterValue.Text;
                NewParameter.ServiceID = Convert.ToInt32(Request.QueryString["ServiceID"].ToString());
                if (chkNewIsDynamic.Checked)
                { NewParameter.IsDynamic = 1; }
                else
                { NewParameter.IsDynamic = 0; }
                CRMBiz.Configuration.WebServiceConfiguration.InsertServiceParameter(NewParameter);
                DataLoad();
            }
        }
        protected void grdConfigurationParameter_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }
        protected void grdConfigurationParameter_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(grdConfigurationParameter.DataKeys[e.RowIndex].Values[0].ToString());
            CRMBiz.Configuration.WebServiceConfiguration.DeleteServiceParamter(id);
            DataLoad();
        }
        protected void grdConfigurationParameter_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdConfigurationParameter.EditIndex = e.NewEditIndex;
            DataLoad();
        }
        protected void grdConfigurationParameter_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            CRMEntity.Configuration.WebServiceParmater UpdateParameter = new CRMEntity.Configuration.WebServiceParmater();

            TextBox txtUpdateParameterName = (TextBox)grdConfigurationParameter.Rows[e.RowIndex].FindControl("txtUpdateParameterName");
            TextBox txtUpdateParameterReplaceString = (TextBox)grdConfigurationParameter.Rows[e.RowIndex].FindControl("txtUpdateParameterReplaceString");
            TextBox txtUpdateParameterValue = (TextBox)grdConfigurationParameter.Rows[e.RowIndex].FindControl("txtUpdateParameterValue");
            CheckBox chkUpdateIsDynamic = (CheckBox)grdConfigurationParameter.Rows[e.RowIndex].FindControl("chkUpdateIsDynamic");

            UpdateParameter.ParameterName = txtUpdateParameterName.Text;
            UpdateParameter.ParameterReplaceString = txtUpdateParameterReplaceString.Text;
            UpdateParameter.ParameterValue = txtUpdateParameterValue.Text;
            UpdateParameter.ParameterID = Convert.ToInt32(grdConfigurationParameter.DataKeys[e.RowIndex].Values[0].ToString());
            UpdateParameter.ServiceID = Convert.ToInt32(Request.QueryString["ServiceID"].ToString());
            if (chkUpdateIsDynamic.Checked)
            { UpdateParameter.IsDynamic = 1; }
            else
            { UpdateParameter.IsDynamic = 0; }
            CRMBiz.Configuration.WebServiceConfiguration.UpdateServiceParameter(UpdateParameter);
            grdConfigurationParameter.EditIndex = -1;
            DataLoad();
        }

        protected void grdConfigurationParameter_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grdConfigurationParameter.EditIndex = -1;
            DataLoad();
        }
    }
}